package com.yzd.learnjava.generics;

import java.lang.reflect.Method;

import com.yzd.learnjava.util.Print;

public class LatenReflection {
	public static void main(String[] args) {
		CommunicatReflectively.perform(new SmartDog());
		CommunicatReflectively.perform(new Robot());
		CommunicatReflectively.perform(new Mine());
	}
}

class Mine{
	public void walkAgainstTheWind() {}
	public void sit() {
		Print.print("Pretending to sit");
	}
	public void pushInvisibleWalls() {}
	public String toString() {
		return "mine";
	}
}

class SmartDog{
	public void speak() {
		Print.print("Woof");
	}
	public void sit() {
		Print.print("Sitting");
	}
	public void reproduce() {}
}

class CommunicatReflectively{
	public static void perform(Object speaker) {
		Class<?> spk = speaker.getClass();
		try {
			try {
				Method m = spk.getMethod("speak");
				m.invoke(speaker);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			try {
				Method sit = spk.getMethod("sit");
				sit.invoke(speaker);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
